local sk__yaohuo = fk.CreateSkill {

  name = "sk__yaohuo",

  tags = {},

}



sk__yaohuo:addEffect("active", {
  name = "sk__yaohuo",
  anim_type = "control",
  min_card_num = 1,
  target_num = 1,
  prompt = "#sk__yaohuo",
  interaction = function(self)
    return UI.ComboBox { choices = { "sk__yaohuo1", "sk__yaohuo2" } }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(sk__yaohuo.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, player, to_select, selected)
    return not player:prohibitDiscard(to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= player then
      if self.interaction.data == "sk__yaohuo1" then
        return not to_select:isKongcheng()
      elseif self.interaction.data == "sk__yaohuo2" then
        local target = to_select
        return #target.player_skills > 0
      end
    end
  end,
  feasible = function(self, player, selected, selected_cards)
    if #selected == 1 then
      local target = selected[1]
      if self.interaction.data == "sk__yaohuo1" then
        return #selected_cards == target:getHandcardNum()
      elseif self.interaction.data == "sk__yaohuo2" then
        return #selected_cards == #table.filter(target.player_skills, function(s)
          return s:isPlayerSkill(target) and s.visible
        end)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:throwCard(effect.cards, sk__yaohuo.name, player, player)
    if player.dead or target.dead then return end
    if self.interaction.data == "sk__yaohuo1" then
      if not target:isKongcheng() then
        room:moveCardTo(target:getCardIds("h"), Card.PlayerHand, player, fk.ReasonPrey, sk__yaohuo.name, nil, false,
          player)
      end
    else
      local all_skills, get = {}, {}
      for _, s in ipairs(target.player_skills) do
        if s:isPlayerSkill(player) and s.visible then
          table.insert(all_skills, s.name)
          if not player:hasSkill(s, true) then
            table.insert(get, s.name)
          end
        end
      end
      if #all_skills == 0 then return end
      local mark = player:getTableMark(sk__yaohuo.name)
      mark[string.format("%.0f", target.id)] = mark[string.format("%.0f", target.id)] or {}
      local m = mark[string.format("%.0f", target.id)]
      m[1] = m[1] or {}
      table.insertTableIfNeed(m[1], all_skills)
      m[2] = m[2] or {}
      table.insertTableIfNeed(m[2], get)
      mark[string.format("%.0f", target.id)] = { m[1], m[2] }
      room:setPlayerMark(player, sk__yaohuo.name, mark)
      room:handleAddLoseSkills(target, "-" .. table.concat(all_skills, "|-"), nil, true, false)
      room:handleAddLoseSkills(player, get, nil, true, false)
    end
  end,
})

sk__yaohuo:addEffect(fk.BeforeTurnStart, {
  name = "#sk__yaohuo_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("sk__yaohuo") ~= 0 then
      local mark = player:getTableMark("sk__yaohuo")
      return mark[string.format("%.0f", target.id)]
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("sk__yaohuo")
    local m = mark[string.format("%.0f", target.id)]
    local all_skills, get = table.simpleClone(m[1]), table.simpleClone(m[2])
    mark[string.format("%.0f", target.id)] = nil
    room:setPlayerMark(player, "sk__yaohuo", mark)
    room:handleAddLoseSkills(target, all_skills, nil, true, false)
    room:handleAddLoseSkills(player, "-" .. table.concat(get, "|-"), nil, true, false)
  end,
})
sk__yaohuo:addEffect(fk.Death, {
  name = "#sk__yaohuo_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("sk__yaohuo") ~= 0 then
      local mark = player:getTableMark("sk__yaohuo")
      return mark[string.format("%.0f", target.id)]
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("sk__yaohuo")
    local m = mark[string.format("%.0f", target.id)]
    local all_skills, get = table.simpleClone(m[1]), table.simpleClone(m[2])
    mark[string.format("%.0f", target.id)] = nil
    room:setPlayerMark(player, "sk__yaohuo", mark)
    room:handleAddLoseSkills(target, all_skills, nil, true, false)
    room:handleAddLoseSkills(player, "-" .. table.concat(get, "|-"), nil, true, false)
  end,
})

return sk__yaohuo
